package com.qire.manhua.activity;

import android.os.Bundle;
import android.view.View;

import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.DeviceConfigInfo;
import com.qire.common.constant.Navigation;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.ThemeUtil;
import com.qire.manhua.R;
import com.qire.manhua.controller.MessageSetupController;
import com.qire.manhua.databinding.ActivityMessageSetupBinding;
import com.qire.manhua.dialog.SystemPushDisablePromptDialog;
import com.qire.manhua.model.viewModel.IMessageSetupViewModel;
import com.qire.other.push.PushManage;

import androidx.annotation.Nullable;

/**
 * 消息设置页
 */
@RouteMapping(path = Navigation.Page.MessageSetupPage)
public class MessageSetupActivity extends BaseActivity<ActivityMessageSetupBinding> {

    @Autowired(injector = ViewModelInjector.class)
    ProxyViewModel<IMessageSetupViewModel> viewModel;

    @Autowired(injector = EventControllerInjector.class)
    MessageSetupController controller;

    @Override
    public int layoutRid() {
        return R.layout.activity_message_setup;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.changeStatusBarLightOrDark(this, true, true);

        controller.onAddition(viewModel);
        refreshUI();

        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.SYNC_PUSH_STATE_INFO, event -> {
            refreshUI();
            return true;
        });

        if(!PushManage.isNotificationEnabled(this)) {
            new SystemPushDisablePromptDialog(this).show();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.SYNC_PUSH_STATE_INFO);
    }

    private void refreshUI() {
        viewModel.set("messagePush", DeviceConfigInfo.messagePushEnable());
        viewModel.set("comicUpdatePush", DeviceConfigInfo.comicUpdatePushEnable());
        viewModel.set("recommendPush", DeviceConfigInfo.recommendPushEnable());
        viewModel.set("commentPush", DeviceConfigInfo.commentPushEnable());
        viewModel.set("detailControlPanelVisible", DeviceConfigInfo.messagePushEnable() ? View.VISIBLE : View.GONE);
    }

}
